#pragma once


///////////////////////////////////////////////////////////////////////////////
//
//  FileName :  rsa.c
//  Version  :  1.0
//  Creator  :  danny
//  Date     :  2010-6-9 10:41:19
//  Comment  :  
//
///////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "mp.h"

UCHAR a_mod[] = {
	0xe0, 0x0b, 0xe4, 0xad, 0x8a, 0x77, 0xee, 0x39, 
	0x44, 0x8e, 0x59, 0x39, 0x63, 0x73,	0x51, 0x19, 
	0x30, 0x4c, 0x58, 0xeb, 0xd8, 0xb6, 0xf0, 0xe4, 
	0x3a, 0xd3, 0x61, 0xc7, 0xc7, 0xc7, 0xf6, 0xdd, 
	0xd8, 0x90, 0xf8, 0xce, 0xe5, 0x94, 0x86, 0x1a, 
	0xde, 0x64, 0xf4, 0x58,	0xff, 0x36, 0x17, 0x0b, 
	0xd3, 0x31, 0x19, 0x6b, 0x68, 0x77, 0xcd, 0x02, 
	0x4d, 0x8d, 0x56, 0xa8, 0x3e, 0x51, 0xfe, 0xc9, 
	0x61, 0x2c, 0xa6, 0x70, 0xe1, 0x0d, 0x6c, 0x23, 
	0xa4, 0xea,	0x02, 0x02, 0x39, 0x9c, 0x06, 0x48, 
	0x85, 0x45, 0x64, 0x46, 0xd9, 0x2b, 0x38, 0x27, 
	0x61, 0x21, 0xd8, 0xe3, 0x09, 0x3f, 0xef, 0x1c, 
	0xbd, 0x90, 0x54, 0xda, 0xcf, 0x33, 0x0f, 0x64,
	0x99, 0x79, 0x17, 0xd3, 0x00, 0x03, 0xe9, 0x91, 
	0x62, 0x35, 0xd7, 0x2e, 0x85, 0xca, 0xe6, 0x2f, 
	0x6f, 0xdc, 0x3d, 0x6c, 0x11, 0xde, 0x1a, 0x83
};

UCHAR a_exp_public[] = {
    0x01, 0x00, 0x01
};



BOOL RSA_Decrypt(
    /*IN*/CONST UCHAR* pCipher, 
    /*IN*/DWORD dwCipherSize, 
    /*IN*/UCHAR *pPlain, 
    /*IN\OUT*/DWORD *pPlainSize
)
{
	BIGNUM *mod, *exp_public;
    BOOL bRet = FALSE;
    BOOL bResult = FALSE;
	BIGNUM *m_enc, *m_dec;

	m_enc = NULL;
	m_dec = NULL;
	mod = NULL;
	exp_public = NULL;
	m_enc = GetPool(sizeof(BIGNUM));
	if (!m_enc){
		CONDITION_ASSERT(bResult);
	}
	m_dec = GetPool(sizeof(BIGNUM));
	if (!m_dec){
		CONDITION_ASSERT(bResult);
	}
	mod = GetPool(sizeof(BIGNUM));
	if (!mod){
		CONDITION_ASSERT(bResult);
	}
	exp_public = GetPool(sizeof(BIGNUM));
	if (!exp_public){
		CONDITION_ASSERT(bResult);
	}
    BN_ASSIGN(m_enc, (UCHAR *)pCipher, dwCipherSize);

    bResult = BN_ASSIGN(mod, a_mod, sizeof(a_mod));
    CONDITION_ASSERT(bResult);

    bResult = BN_ASSIGN(exp_public, a_exp_public, sizeof(a_exp_public));
    CONDITION_ASSERT(bResult);

    bResult = BN_EXPMOD(m_dec, m_enc, exp_public, mod);
    CONDITION_ASSERT(bResult);

    bResult = BN_REVERT(m_dec, pPlain, *pPlainSize);;
    CONDITION_ASSERT(bResult);

    bRet = TRUE;
Exit0:
	if (m_enc){
		ExFreePool(m_enc);
	}
	if (m_dec){
		ExFreePool(m_dec);
	}
	if (mod){
		ExFreePool(mod);
	}
	if (exp_public){
		ExFreePool(exp_public);
	}
    return bRet;
}